<?php
namespace app\reportExport\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;

class CopyPaperApproval extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$page = input('page');
		$rows = input('rows');
		$startDate = input("startDate");
		$endDate = input("endDate");
		$approvalStatus = input("approvalStatus");
		$where["wa.IS_DELETE"] = "N";
		$where["APPROVAL_TYPE"] = "copyPaper";
		if($approvalStatus) $where["APPROVAL_STATUS"] = array("eq",$approvalStatus);
		if($startDate && $endDate) {
			$where["APPLY_TIME"] = array("between",[$startDate,$endDate]);
		}else {
			if($startDate) $where["APPLY_TIME"] = array("egt", $startDate);
			if($endDate) $where["APPLY_TIME"] = array("elt", $endDate);
		}
		$result = Db::table("t_work_approval wa")
				  ->join("t_work_approval_item wai","wa.ID = wai.APPLY_ID","INNER")
				  ->field("wa.ID id,wa.APPLYER,wa.APPLYER_ID,wa.APPROVER,wa.APPROVER_ID,wa.CC_LIST,wa.APPROVAL_STATUS,wa.APPLY_TIME,wa.APPROVAL_TIME,wa.DEPART,wai.REMARK,wa.REJECT_REASON,wa.APPROVAL_TYPE,wai.USE_POSITION,wai.COPY_CONTENT,wai.APPLY_ITEMS")
				  ->where($where)
				  ->page($page, $rows)
				  ->order("APPLY_TIME desc,APPROVAL_TIME desc")
				  ->select();
		$data = [];
		foreach ($result as $key => $obj) {
            $status = $obj["APPROVAL_STATUS"];
            if("000" == $status) $obj["APPROVAL_STATUS_TEXT"] = "审批通过";
            else if("001" == $status) $obj["APPROVAL_STATUS_TEXT"] = "待审批";
            else if("002" == $status) $obj["APPROVAL_STATUS_TEXT"] = "审批不通过";
            else if("003" == $status) $obj["APPROVAL_STATUS_TEXT"] = "已撤销";
			$data[$key] = $obj;
		}
		$count = Db::table("t_work_approval wa")
				  ->join("t_work_approval_item wai","wa.ID = wai.APPLY_ID","INNER")
				  ->where($where)
				  ->count();
		$grid["rows"] = $data;
		$grid["total"] = $count;
		return $grid;
	}

	public function exportExcel() {
		$startDate = input("startDate");
		$endDate = input("endDate");
		$approvalStatus = input("approvalStatus");
		$where["wa.IS_DELETE"] = "N";
		$where["APPROVAL_TYPE"] = "copyPaper";
		if($approvalStatus) $where["APPROVAL_STATUS"] = array("eq",$approvalStatus);
		if($startDate && $endDate) {
			$where["APPLY_TIME"] = array("between",[$startDate,$endDate]);
		}else {
			if($startDate) $where["APPLY_TIME"] = array("egt", $startDate);
			if($endDate) $where["APPLY_TIME"] = array("elt", $endDate);
		}
		$result = Db::table("t_work_approval wa")
				  ->join("t_work_approval_item wai","wa.ID = wai.APPLY_ID","INNER")
				  ->field("wa.ID id,wa.APPLYER,wa.APPLYER_ID,wa.APPROVER,wa.APPROVER_ID,wa.CC_LIST,wa.APPROVAL_STATUS,wa.APPLY_TIME,wa.APPROVAL_TIME,wa.DEPART,wai.REMARK,wa.REJECT_REASON,wa.APPROVAL_TYPE,wai.USE_POSITION,wai.COPY_CONTENT,wai.APPLY_ITEMS")
				  ->where($where)
				  ->order("APPLY_TIME desc,APPROVAL_TIME desc")
				  ->select();
		$data = [];
		foreach ($result as $key => $obj) {
            $status = $obj["APPROVAL_STATUS"];
            if("000" == $status) $obj["APPROVAL_STATUS_TEXT"] = "审批通过";
            else if("001" == $status) $obj["APPROVAL_STATUS_TEXT"] = "待审批";
            else if("002" == $status) $obj["APPROVAL_STATUS_TEXT"] = "审批不通过";
            else if("003" == $status) $obj["APPROVAL_STATUS_TEXT"] = "已撤销";
			$data[$key] = $obj;
		}
		//excel表头内容
		$header = array("部门"=>"DEPART","审批明细"=>"APPLY_ITEMS","申请人"=>"APPLYER","申请时间"=>"APPLY_TIME","审批状态"=>"APPROVAL_STATUS_TEXT","审批人"=>"APPROVER","审批时间"=>"APPROVAL_TIME","驳回原因"=>"REJECT_REASON","备注"=>"REMARK");
		Excel::toExcel($data,'复印油印申请表',$header);
		exit;
	}

}
